package com.example.demolistview;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MessageAdapter extends ArrayAdapter<String> {
	private String[] mMessageList;
	private Context mContext;

	public MessageAdapter(Context context, String[] mMessageList) {
		super(context, R.layout.item_message,mMessageList);
		this.mMessageList = mMessageList;
		this.mContext = context;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View row = convertView;
		try {
			// using view holder to holde row
			ViewHolder viewHolder;
			if (row == null) {
				LayoutInflater inflater = (LayoutInflater) mContext
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				row = inflater.inflate(R.layout.item_message, null);

				viewHolder = new ViewHolder();
				viewHolder.text = (TextView) row.findViewById(R.id.tvMessage);
				viewHolder.image = (ImageView) row
						.findViewById(R.id.imgViewItem);
				row.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}

			// set data for view
			viewHolder.text.setText(mMessageList[position]);
			viewHolder.image.setImageResource(R.drawable.messageicon);

		} catch (Exception e) {
			Log.e("Loi",e.toString());
		}

		return row;

	}

	class ViewHolder {
		public TextView text;
		public ImageView image;
	}
}
